package com.evil.application.pojo.dto.reporting;

import com.alibaba.fastjson.JSON;
import com.evil.application.enums.reporting.ReportingStyleTypeEnum;
import com.evil.application.pojo.dto.form.control.code.config.FilterConfigDTO;
import com.evil.application.pojo.dto.permission.control.ControlPermissionDTO;
import com.evil.application.pojo.entity.ApplicationReporting;
import com.evil.common.application.dto.order.OrderQueryReqDTO;
import com.evil.common.application.enums.QueryTypeEnum;
import com.evil.common.core.enums.SwitchEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

/**
 * 应用报表 返回实体
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ApplicationReportingDTO extends ApplicationReportingBaseDTO {

    /**
     * 报表维度（行、表头）
     */
    private List<ReportingHeaderDTO> reportingHeaders;

    /**
     * 报表维度（列）
     */
    private List<ReportingHeaderDTO> reportingColumnHeaders;

    /**
     * 指标
     */
    private List<ReportingTargetDTO> reportingTargets;

    /**
     * 排序开关(0-关闭, 1-开启)
     */
    private Integer sortStatus;

    /**
     * 排序配置
     */
    private List<ReportingSortDTO> sorts;

    /**
     * 排名(0-关闭, 1-开启)
     */
    private Integer rankingStatus;

    /**
     * 排名配置
     */
    @Valid
    private ReportingRankingConfigDTO rankingConfig;

    /**
     * 样式类型（0-默认，1-蓝橙撞色，2-英伦酒红，3-清新草绿，4-神秘魅紫，5-热情暖阳）
     */
    private Integer styleType;

    /**
     * 操作权限配置
     */
    private ReportingPermissionConfigDTO permissionConfig;

    /**
     * 筛选条件开关(0-关闭, 1-开启)
     */
    private Integer filterStatus;

    /**
     * 筛选条件
     */
    private FilterConfigDTO filterConfig;

    /**
     * 查询条件开关(0-关闭, 1-开启)
     */
    private Integer queryStatus;

    /**
     * 查询条件类型(0-精确查询, 1-模糊查询)
     */
    private Integer queryType;

    /**
     * 查询条件
     */
    private QueryConfigDTO queryConfig;

    /**
     * 筛选参数
     */
    @Valid
    private List<OrderQueryReqDTO> queries;

    /**
     * 控件权限开关(0-关闭, 1-开启)
     */
    private Integer controlPermissionStatus;

    /**
     * 控件权限
     */
    private List<ControlPermissionDTO> controlPermissions;

    public ApplicationReportingDTO() {
        super();
        this.reportingHeaders = new ArrayList<>();
        this.reportingColumnHeaders = new ArrayList<>();
        this.reportingTargets = new ArrayList<>();
        this.sortStatus = SwitchEnum.NO.getId();
        this.sorts = new ArrayList<>();
        this.rankingStatus = SwitchEnum.NO.getId();
        this.rankingConfig = new ReportingRankingConfigDTO();
        this.permissionConfig = new ReportingPermissionConfigDTO();
        this.styleType = ReportingStyleTypeEnum.DEFAULT.getId();
        this.filterStatus = SwitchEnum.YES.getId();
        this.filterConfig = new FilterConfigDTO();
        this.queryStatus = SwitchEnum.NO.getId();
        this.queryType = QueryTypeEnum.ACCURATE.getId();
        this.queryConfig = new QueryConfigDTO();
        this.queries = new ArrayList<>();
        this.controlPermissionStatus = SwitchEnum.NO.getId();
        this.controlPermissions = new ArrayList<>();
    }

    public ApplicationReportingDTO(ApplicationReporting applicationReporting, Function<Long, Boolean> beingTableChild) {
        super(applicationReporting);
        // 报表维度（行、表头）
        List<ReportingHeaderDTO> reportingHeaders = JSON.parseArray(applicationReporting.getReportingHeaders(), ReportingHeaderDTO.class);
        this.reportingHeaders = Objects.nonNull(reportingHeaders) ? reportingHeaders : new ArrayList<>();
        this.reportingHeaders.forEach(e -> e.setBeingTableControl(beingTableChild.apply(e.getControlId())));
        // 报表维度（列）
        List<ReportingHeaderDTO> reportingColumnHeaders = JSON.parseArray(applicationReporting.getReportingColumnHeaders(), ReportingHeaderDTO.class);
        this.reportingColumnHeaders = Objects.nonNull(reportingColumnHeaders) ? reportingColumnHeaders : new ArrayList<>();
        this.reportingColumnHeaders.forEach(e -> e.setBeingTableControl(beingTableChild.apply(e.getControlId())));
        // 指标
        List<ReportingTargetDTO> reportingTargets = JSON.parseArray(applicationReporting.getReportingTargets(), ReportingTargetDTO.class);
        this.reportingTargets = Objects.nonNull(reportingTargets) ? reportingTargets : new ArrayList<>();
        this.reportingTargets.forEach(e -> e.setBeingTableControl(beingTableChild.apply(e.getControlId())));
        // 排序开关
        this.sortStatus = applicationReporting.getSortStatus();
        // 排序配置
        List<ReportingSortDTO> sorts = JSON.parseArray(applicationReporting.getSorts(), ReportingSortDTO.class);
        this.sorts = Objects.nonNull(sorts) ? sorts : new ArrayList<>();
        // 排名开关
        this.rankingStatus = applicationReporting.getRankingStatus();
        // 排名配置
        ReportingRankingConfigDTO rankingConfig = JSON.parseObject(applicationReporting.getRankingConfig(), ReportingRankingConfigDTO.class);
        this.rankingConfig = Objects.nonNull(rankingConfig) ? rankingConfig : new ReportingRankingConfigDTO();
        // 操作权限配置
        ReportingPermissionConfigDTO permissionConfig = JSON.parseObject(applicationReporting.getPermissionConfig(), ReportingPermissionConfigDTO.class);
        this.permissionConfig = Objects.nonNull(permissionConfig) ? permissionConfig : new ReportingPermissionConfigDTO();
        // 样式类型
        this.styleType = applicationReporting.getStyleType();
        // 筛选条件开关
        this.filterStatus = SwitchEnum.YES.getId();
        // 筛选条件
        this.filterConfig = new FilterConfigDTO();
        // 查询条件开关
        this.queryStatus = applicationReporting.getQueryStatus();
        // 查询条件类型
        this.queryType = applicationReporting.getQueryType();
        // 查询条件
        QueryConfigDTO queryConfig = JSON.parseObject(applicationReporting.getQueryConfig(), QueryConfigDTO.class);
        this.queryConfig = Objects.nonNull(queryConfig) ? queryConfig : new QueryConfigDTO();
        this.queries = new ArrayList<>();
        // 控件权限开关
        this.controlPermissionStatus = applicationReporting.getControlPermissionStatus();
    }

    public ApplicationReporting createApplicationReporting() {
        ApplicationReporting applicationReporting = super.createApplicationReporting();
        // 复制属性
        this.copyProps(applicationReporting);

        return applicationReporting;
    }

    public void copyProps(ApplicationReporting applicationReporting) {
        super.copyProps(applicationReporting);

        applicationReporting.setReportingHeaders(JSON.toJSONString(this.reportingHeaders));
        applicationReporting.setReportingColumnHeaders(JSON.toJSONString(this.reportingColumnHeaders));
        applicationReporting.setReportingTargets(JSON.toJSONString(this.reportingTargets));
        applicationReporting.setSortStatus(this.sortStatus);
        applicationReporting.setSorts(JSON.toJSONString(this.sorts));
        applicationReporting.setRankingStatus(this.rankingStatus);
        applicationReporting.setRankingConfig(JSON.toJSONString(this.rankingConfig));
        applicationReporting.setStyleType(this.styleType);
        applicationReporting.setPermissionConfig(JSON.toJSONString(this.permissionConfig));
        applicationReporting.setFilterStatus(this.filterStatus);
        applicationReporting.setQueryStatus(this.queryStatus);
        applicationReporting.setQueryType(this.queryType);
        applicationReporting.setQueryConfig(JSON.toJSONString(this.queryConfig));
        applicationReporting.setControlPermissionStatus(this.controlPermissionStatus);
    }
}
